package atm.service;

import atm.domain.Account;
import atm.domain.Customer;

import java.util.HashMap;
import java.util.Map;

public class AccountService {

    private Map<String, Account> accountList = new HashMap<String, Account>();

    public AccountService() {
        Customer customer1 = new Customer("小红", "01");
        Customer customer2 = new Customer("小绿", "02");
        Customer customer3 = new Customer("小兰", "03");
        Account account1 = new Account("01", customer1, "123", 20000);
        Account account2 = new Account("02", customer2, "123", 30000);
        Account account3 = new Account("03", customer3, "123", 40000);
        accountList.put("01", account1);
        accountList.put("02", account2);
        accountList.put("03", account3);
    }


    //验证账户
    public boolean validateAccount(String cardid, String password) {
    	if(accountList.get(cardid) == null)
    		return false;
    	else
    	    return password != null && password.equals(accountList.get(cardid).getPassword());
    }


    //判断取款金额是否充足
    public Boolean withdrawal(String cardid, int amount) {
        Account account = accountList.get(cardid);
        if(amount%100==0) {
        	if (amount <= account.getBalance()) {
                int balance = account.getBalance() - amount;
                account.setBalance(balance);
                System.out.println("本次取得：" + amount + "元");
                System.out.println("账户余额：" + account.getBalance() + "元");
                return true;
            }
        	
        	else {
                System.out.println("取款失败！账户余额不足！");
                return false;
            }
        }
        
        else {
        	System.out.print("取款失败！取款金额必须是100的倍数！");
        	return false;
        }
    }


}
